package guiClasses;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;

import objectClasses.User;
import objectClasses.UserInfo;

public class AttendeeListPanel extends GenericPanel
{
	public AttendeeListPanel(Observer the_o_er)
	{
		super(the_o_er);
		update(the_o_er);
	}
	
	public void update(final Observer the_o_er)
	{
		JLabel att = new JLabel("Attendees");
		JLabel space = new JLabel(" ");
		Font f = new Font(att.getFont().getName(),Font.BOLD, 20);  
		att.setFont(f);
		my_panel.add(att, GBC.GBCGen(0, 0, 0, 1));
		my_panel.add(space, GBC.GBCGen(0, 0, 0, 2));
		ArrayList<UserInfo> all_users;
		try 
		{
			all_users = User.getAllUserInfo();
			ArrayList<UserInfo> attendees = new ArrayList<UserInfo>();
			for(int i = 0; i < all_users.size(); i++)
			{
				if(all_users.get(i).getUserType() == 'A')
				{
					attendees.add(all_users.get(i));
				}
			}
			JPanel list = new JPanel(new GridBagLayout());
			JButton[] buttons = new JButton[attendees.size()];
			for(int i = 0; i < attendees.size(); i++)
			{
				final UserInfo a = attendees.get(i);
				JButton temp = new JButton();
				temp.setForeground(Color.BLUE);
				temp.setText(a.getUserName());
				temp.setBorderPainted(false);
	            temp.setOpaque(false);
	            temp.setBackground(new Color(139, 101, 219));
	            temp.addActionListener(new ActionListener() 
	            {
	                public void actionPerformed(ActionEvent e) 
	                {
	                	setChanged();
	    				notifyObservers(new InfoPanel(the_o_er, a).my_panel);
	                }
	            });
	            temp.setCursor(new Cursor(Cursor.HAND_CURSOR));
	            buttons[i] = temp;
			}
			for(int i = 0; i < buttons.length; i++)
			{
				list.add(buttons[i], GBC.GBCGen(0, 0, i/6, i % 6));
			}
			JScrollPane scrollPane = new JScrollPane(list);
			scrollPane.setPreferredSize(new Dimension(436, 190));
			my_panel.add(scrollPane, GBC.GBCGen(0, 0, 0, 3));
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		my_panel.setBorder(new TitledBorder("Event Attendee List"));
	}
}
